package com.burstly.lib.component.networkcomponent.burstly.html.mraid;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Build.VERSION;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.burstly.lib.component.networkcomponent.burstly.OverlayContainer;
import com.burstly.lib.component.networkcomponent.burstly.html.ICloseControl;
import com.burstly.lib.component.networkcomponent.burstly.html.IRichMediaAdListener;
import com.burstly.lib.component.networkcomponent.burstly.html.IWrappedContentProvider;
import com.burstly.lib.component.networkcomponent.burstly.html.LoadingAwareWebView;
import com.burstly.lib.component.networkcomponent.burstly.wrapper.WebViewClientWrapper;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.ScreenOrientationManager;
import com.burstly.lib.util.Utils;
import java.util.HashMap;
import java.util.Map;

public class MraidView extends LoadingAwareWebView
  implements Runnable
{
  static final String INLINE_PLACEMENT_TYPE = "inline";
  static final String INTERSTITIAL_PLACEMENT_TYPE = "interstitial";
  static final LoggerExt LOG = LoggerExt.getInstance();
  static final String TAG = MraidView.class.getSimpleName();
  public static final String VERSION = "2.0";
  private ICloseControl mCloseButtonControl;
  private long mCloseButtonShowDelay;
  private BroadcastReceiver mConfigurationReciever;
  private ViewGroup mContentParent;
  private IWrappedContentProvider mContentProvider;
  final MraidEventBus<MraidView> mEventBus = new MraidEventBus(this);
  private JavascriptEventFactory mEventFactory;
  private boolean mLazyShowCustomClose;
  private OverlayContainer<?> mOpenedOverlay;
  volatile boolean mPageLoaded;
  private int mPrevPositionX;
  private int mPrevPositionY;
  volatile boolean mPublishedBridgeLoaded;
  IRichMediaAdListener mRichMediaListener;
  private ScreenOrientationManager mScreenOrientationMgr;
  private Handler mUiHandler;
  final MraidViewState mViewState = new MraidViewState();

  @TargetApi(11)
  public MraidView(Context paramContext)
  {
    super(paramContext);
    if (Build.VERSION.SDK_INT >= 11)
    {
      setLayerType(2, null);
      getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    }
    this.mEventFactory = new JavascriptEventFactory("mraidview");
    getSettings().setJavaScriptEnabled(true);
    getSettings().setPluginsEnabled(true);
    setFocusableInTouchMode(true);
    setScrollContainer(false);
    setVerticalScrollBarEnabled(false);
    setHorizontalScrollBarEnabled(false);
    addJavascriptInterface(this, "burstlyBridge");
    setWebViewClient(new WebViewClient());
    setWebChromeClient(new WebChromeClient());
    this.mContentParent = ((ViewGroup)((Activity)paramContext).findViewById(16908290));
    if (this.mContentParent == null)
      this.mContentParent = ((ViewGroup)((Activity)paramContext).getWindow().getDecorView());
  }

  private void fireEvent(String paramString)
  {
    if ((this.mPageLoaded) && (!wasDestroyed()))
      executeScriptUnSafe(paramString);
  }

  private void startConfigurationListener()
  {
    try
    {
      if (this.mConfigurationReciever == null)
      {
        this.mConfigurationReciever = new ConfigurationBroadcastReceiver(this);
        getContext().registerReceiver(this.mConfigurationReciever, new IntentFilter("android.intent.action.CONFIGURATION_CHANGED"));
      }
      return;
    }
    catch (Exception localException)
    {
      LOG.logThrowable(TAG, localException);
    }
  }

  private void stopConfigurationListener()
  {
    if (this.mConfigurationReciever != null);
    try
    {
      getContext().unregisterReceiver(this.mConfigurationReciever);
      this.mConfigurationReciever = null;
      return;
    }
    catch (Exception localException)
    {
      LOG.logThrowable(TAG, localException);
    }
  }

  void addCurrentPositionValue(Map<String, Object> paramMap)
  {
    int[] arrayOfInt1 = new int[2];
    this.mContentParent.getLocationInWindow(arrayOfInt1);
    int[] arrayOfInt2 = new int[2];
    getLocationInWindow(arrayOfInt2);
    this.mPrevPositionX = Utils.scaleToDip(Integer.valueOf(arrayOfInt2[0] - arrayOfInt1[0])).intValue();
    this.mPrevPositionY = Utils.scaleToDip(Integer.valueOf(arrayOfInt2[1] - arrayOfInt1[1])).intValue();
    paramMap.put("currentPosition", "{x: " + this.mPrevPositionX + ", y:" + this.mPrevPositionY + ", width:" + Utils.scaleToDip(Integer.valueOf(getMeasuredWidth())) + ", height: " + Utils.scaleToDip(Integer.valueOf(getMeasuredHeight())) + "}");
  }

  void addMaxSizeValue(Map<String, Object> paramMap)
  {
    String str = getMaxSize();
    if (str != null)
      paramMap.put("maxSize", str);
  }

  void addOrientationValue(Map<String, Object> paramMap)
  {
    paramMap.put("orientation", Integer.valueOf(Utils.getOrientation(getContext())));
  }

  void addPlacementTypeValue(Map<String, Object> paramMap)
  {
    if (isInterstitial());
    for (String str = "interstitial"; ; str = "inline")
    {
      paramMap.put("placementType", str);
      return;
    }
  }

  void addScreenSizeValue(Map<String, Object> paramMap)
  {
    paramMap.put("screenSize", getScreenSize());
  }

  void addStateValue(Map<String, Object> paramMap)
  {
    paramMap.put("state", this.mViewState.getState().name().toLowerCase());
  }

  void addSupportedFeatures(Map<String, Object> paramMap)
  {
    paramMap.put("supports", DevaceCapabilitiesManager.getCapabilities(getContext()));
  }

  void addViewableValue(Map<String, Object> paramMap)
  {
    paramMap.put("viewable", Boolean.valueOf(isShown()));
  }

  @JavascriptInterface
  public void changeOrientationProperiesInLive(String paramString)
  {
    sendEvent(12, new Object[] { paramString });
  }

  @JavascriptInterface
  public void close()
  {
    sendEvent(2, new Object[0]);
  }

  @Deprecated
  public void closeExternally()
  {
    if ((this.mViewState.getState() == MraidBridgeState.LOADING) && (isInterstitial()))
    {
      ((Activity)getContext()).finish();
      return;
    }
    close();
  }

  @JavascriptInterface
  public void createCalendarEvent(String paramString)
  {
    sendEvent(8, new Object[] { paramString });
  }

  void executeScriptUnSafe(String paramString)
  {
    loadUrl("javascript:" + paramString);
  }

  @JavascriptInterface
  public void expand(String paramString1, String paramString2)
  {
    sendEvent(3, new Object[] { paramString2, paramString1 });
  }

  void fireComplexChangeEvent(Map<String, Object> paramMap)
  {
    fireEvent(this.mEventFactory.createChangeEvent(paramMap));
  }

  void fireErrorEvent(String paramString1, String paramString2)
  {
    fireEvent(this.mEventFactory.createErrorEvent(paramString1, paramString2));
  }

  void fireSimpleChangeEvent(String paramString, Object paramObject)
  {
    fireEvent(this.mEventFactory.createSimpleChangeEvent(paramString, paramObject));
  }

  void fireVisibilityRelatedComplexEvent(boolean paramBoolean)
  {
    if (this.mPageLoaded)
    {
      HashMap localHashMap = new HashMap();
      if (paramBoolean)
        addStateValue(localHashMap);
      addViewableValue(localHashMap);
      addCurrentPositionValue(localHashMap);
      fireComplexChangeEvent(localHashMap);
    }
  }

  public ICloseControl getCloseButtonControl()
  {
    return this.mCloseButtonControl;
  }

  public long getCloseButtonShowDelay()
  {
    return this.mCloseButtonShowDelay;
  }

  public ViewGroup getContentParent()
  {
    return this.mContentParent;
  }

  String getMaxSize()
  {
    int i = Utils.scaleToDip(Integer.valueOf(this.mContentParent.getWidth())).intValue();
    int j = Utils.scaleToDip(Integer.valueOf(this.mContentParent.getHeight())).intValue();
    return "{width: " + i + ", height: " + j + "}";
  }

  public OverlayContainer<?> getOpenedOverlay()
  {
    return this.mOpenedOverlay;
  }

  public IRichMediaAdListener getRichMediaListener()
  {
    return this.mRichMediaListener;
  }

  ScreenOrientationManager getScreenOrientationMgr()
  {
    return this.mScreenOrientationMgr;
  }

  String getScreenSize()
  {
    Context localContext = getContext();
    int i = Utils.getScreenWidth(localContext);
    int j = Utils.getScreenHeight(localContext);
    return "{width:" + Utils.scaleToDip(Integer.valueOf(i)) + ", height:" + Utils.scaleToDip(Integer.valueOf(j)) + "}";
  }

  String getViewSize()
  {
    return "{width: " + getMeasuredWidth() + ", height: " + getMeasuredHeight() + "}";
  }

  public void loadDataWithBaseURL(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    if (this.mContentProvider == null)
      this.mContentProvider = new MraidWrappedContentProvider();
    super.loadDataWithBaseURL(paramString1, this.mContentProvider.prepareWrappedContent(paramString2), paramString3, paramString4, paramString5);
  }

  protected void onAttachedToWindow()
  {
    super.onAttachedToWindow();
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Boolean.valueOf(true);
    sendEvent(5, arrayOfObject);
    startConfigurationListener();
  }

  public void onDestroy(Activity paramActivity)
  {
    if (!wasDestroyed());
    try
    {
      super.onDestroy(paramActivity);
      this.mViewState.setState(MraidBridgeState.HIDDEN);
      fireVisibilityRelatedComplexEvent(true);
      stopConfigurationListener();
      if (this.mContentProvider != null)
        this.mContentProvider.destroy();
      this.mEventBus.destroy();
      destroy();
      return;
    }
    catch (Exception localException)
    {
      LOG.logThrowable(TAG, localException);
    }
  }

  protected void onDetachedFromWindow()
  {
    super.onDetachedFromWindow();
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Boolean.valueOf(false);
    sendEvent(5, arrayOfObject);
    stopConfigurationListener();
  }

  protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
    if (paramBoolean)
      sendEvent(7, new Object[0]);
  }

  protected void onVisibilityChanged(View paramView, int paramInt)
  {
    super.onVisibilityChanged(paramView, paramInt);
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Integer.valueOf(paramInt);
    sendEvent(4, arrayOfObject);
  }

  @JavascriptInterface
  public void open(String paramString)
  {
    sendEvent(1, new Object[] { paramString });
  }

  @JavascriptInterface
  public void playVideo(String paramString)
  {
    sendEvent(11, new Object[] { paramString });
  }

  void publishBridgeLoaded()
  {
    if ((this.mPageLoaded) && (!this.mPublishedBridgeLoaded))
    {
      HashMap localHashMap = new HashMap();
      addStateValue(localHashMap);
      addPlacementTypeValue(localHashMap);
      addCurrentPositionValue(localHashMap);
      addViewableValue(localHashMap);
      addMaxSizeValue(localHashMap);
      addScreenSizeValue(localHashMap);
      addSupportedFeatures(localHashMap);
      addOrientationValue(localHashMap);
      fireComplexChangeEvent(localHashMap);
      executeScriptUnSafe(this.mEventFactory.createReadyEvent());
      this.mPublishedBridgeLoaded = true;
    }
  }

  @JavascriptInterface
  public void resize(String paramString)
  {
    sendEvent(10, new Object[] { paramString });
  }

  public void run()
  {
    if (!wasDestroyed())
    {
      this.mUiHandler.postDelayed(this, 1000L);
      if (isShown())
      {
        int[] arrayOfInt1 = new int[2];
        this.mContentParent.getLocationInWindow(arrayOfInt1);
        HashMap localHashMap = new HashMap();
        int[] arrayOfInt2 = new int[2];
        getLocationInWindow(arrayOfInt2);
        int i = Utils.scaleToDip(Integer.valueOf(arrayOfInt2[0] - arrayOfInt1[0])).intValue();
        int j = Utils.scaleToDip(Integer.valueOf(arrayOfInt2[1] - arrayOfInt1[1])).intValue();
        if ((this.mPrevPositionX != i) || (this.mPrevPositionY != j))
        {
          this.mPrevPositionY = i;
          this.mPrevPositionY = j;
          localHashMap.put("currentPosition", "{x: " + i + ", y:" + j + "}");
          fireComplexChangeEvent(localHashMap);
          this.mPrevPositionX = i;
        }
      }
    }
  }

  void sendEvent(int paramInt, Object[] paramArrayOfObject)
  {
    this.mEventBus.sendEvent(paramInt, paramArrayOfObject);
  }

  public void setCloseButtonControl(ICloseControl paramICloseControl)
  {
    this.mCloseButtonControl = paramICloseControl;
    if (this.mLazyShowCustomClose)
    {
      useCustomClose(this.mLazyShowCustomClose);
      this.mLazyShowCustomClose = false;
    }
  }

  public void setCloseButtonShowDelay(long paramLong)
  {
    this.mCloseButtonShowDelay = paramLong;
  }

  public void setContentParent(ViewGroup paramViewGroup)
  {
    this.mContentParent = paramViewGroup;
  }

  public void setOpenedOverlay(OverlayContainer<?> paramOverlayContainer)
  {
    this.mOpenedOverlay = paramOverlayContainer;
  }

  public void setRichMediaListener(IRichMediaAdListener paramIRichMediaAdListener)
  {
    this.mRichMediaListener = paramIRichMediaAdListener;
  }

  void setScreenOrientationMgr(ScreenOrientationManager paramScreenOrientationManager)
  {
    this.mScreenOrientationMgr = paramScreenOrientationManager;
  }

  public void setWebViewClient(WebViewClient paramWebViewClient)
  {
    super.setWebViewClient(new WebClient(paramWebViewClient));
  }

  void startPositionNotification()
  {
    if (this.mUiHandler == null)
      this.mUiHandler = new Handler();
    this.mUiHandler.postDelayed(this, 1000L);
  }

  void stopPositionNotification()
  {
    if (this.mUiHandler != null)
      this.mUiHandler.removeCallbacks(this);
  }

  @JavascriptInterface
  public void storePicture(String paramString)
  {
    sendEvent(9, new Object[] { paramString });
  }

  @JavascriptInterface
  public void useCustomClose(final boolean paramBoolean)
  {
    final ICloseControl localICloseControl = this.mCloseButtonControl;
    if (localICloseControl != null)
    {
      post(new Runnable()
      {
        public void run()
        {
          ICloseControl localICloseControl = localICloseControl;
          if (!paramBoolean);
          for (boolean bool = true; ; bool = false)
          {
            localICloseControl.showClose(bool);
            return;
          }
        }
      });
      return;
    }
    this.mLazyShowCustomClose = paramBoolean;
  }

  private class WebClient extends WebViewClientWrapper
  {
    WebClient(WebViewClient arg2)
    {
      super();
    }

    public void onPageFinished(WebView paramWebView, String paramString)
    {
      super.onPageFinished(paramWebView, paramString);
      MraidView.this.sendEvent(0, new Object[0]);
    }

    public void onPageStarted(WebView paramWebView, String paramString, Bitmap paramBitmap)
    {
      super.onPageStarted(paramWebView, paramString, paramBitmap);
      MraidView.this.sendEvent(6, new Object[0]);
    }

    public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString)
    {
      if (MraidView.this.mRichMediaListener != null)
      {
        MraidView.this.mRichMediaListener.onClick(paramString);
        return true;
      }
      if ((!URLUtil.isHttpsUrl(paramString)) && (!URLUtil.isHttpsUrl(paramString)))
      {
        MraidView.this.open(paramString);
        return true;
      }
      return super.shouldOverrideUrlLoading(paramWebView, paramString);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.networkcomponent.burstly.html.mraid.MraidView
 * JD-Core Version:    0.6.2
 */